package com.typedef.basichumingbirdcontroller;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import com.hoho.android.usbserial.util.HexDump;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import com.typedef.helper.HelperContainer;
import com.typedef.helper.UsbConectionHelper;

import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class USBConnectionFragment extends Fragment implements OnClickListener {
	
	public static final String ARG_SECTION_NUMBER = "section_number";
	private final String TAG = USBConnectionFragment.class.getSimpleName();
    
    private TextView statusTextView = null;
    private Button connectButton = null;
    private Button disconnectButton = null;
    private Spinner spinnerBaudrate, spinnerDatabits, spinnerStopbits, spinnerParity;
    private UsbConectionHelper usbConnHelper = null;

	public USBConnectionFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_main_usbconnection, container, false);
		return rootView;
	}
	
	@Override
	public void onPause() {
        super.onPause();
    }
	
	@Override
	public void onResume() {
        super.onResume();
     	statusTextView = (TextView) this.getActivity().findViewById(R.id.textView_connection_status);
     	connectButton = (Button) this.getActivity().findViewById(R.id.button_connect);
     	connectButton.setOnClickListener(this);
     	disconnectButton = (Button) this.getActivity().findViewById(R.id.button_disconnect);
     	disconnectButton.setOnClickListener(this);
        
     	// Spinners
     	spinnerBaudrate = (Spinner) this.getActivity().findViewById(R.id.spinner_baudrate);
     	spinnerBaudrate.setSelection(((ArrayAdapter) spinnerBaudrate.getAdapter()).getPosition("57600"));
     	
     	spinnerDatabits = (Spinner) this.getActivity().findViewById(R.id.spinner_databits);
     	spinnerDatabits.setSelection(((ArrayAdapter) spinnerDatabits.getAdapter()).getPosition("8"));
     	
     	spinnerStopbits = (Spinner) this.getActivity().findViewById(R.id.spinner_stopbits);
     	spinnerStopbits.setSelection(((ArrayAdapter) spinnerStopbits.getAdapter()).getPosition("1"));
     	
     	spinnerParity = (Spinner) this.getActivity().findViewById(R.id.spinner_parity);
     	spinnerParity.setSelection(((ArrayAdapter) spinnerParity.getAdapter()).getPosition("0"));
        
        //onDeviceStateChange();
    }

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.button_connect){
			if(this.usbConnHelper == null){
				if(HelperContainer.getObject(HelperContainer.USB_CONN_HELPER) == null){
					this.usbConnHelper = new UsbConectionHelper(
							getActivity(),
							Integer.parseInt(spinnerBaudrate.getSelectedItem().toString()), 
							Integer.parseInt(spinnerDatabits.getSelectedItem().toString()), 
							Integer.parseInt(spinnerStopbits.getSelectedItem().toString()), 
							Integer.parseInt(spinnerParity.getSelectedItem().toString()));
				}
				else{
					this.usbConnHelper = (UsbConectionHelper) HelperContainer.getObject(HelperContainer.USB_CONN_HELPER);
				}
			}
			statusTextView.setText(this.usbConnHelper.createConn());
			HelperContainer.setObject(HelperContainer.USB_CONN_HELPER, this.usbConnHelper);
		}
		else if(arg0.getId() == R.id.button_disconnect){
			if (this.usbConnHelper != null) {
	            Log.i(TAG, "Stopping io manager ..");
	            this.usbConnHelper.stopDevice();
	        }
			statusTextView.setText(R.string.frag_conn_not_connected);
			connectButton.setVisibility(View.VISIBLE);
			disconnectButton.setVisibility(View.INVISIBLE);
		}
	}
}